
package com.kugou.game.sdk.ui.widget;

import com.kugou.game.sdk.api.common.ActivityOrientation;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.utils.Rx;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 描述:信息ITEM控件
 *
 * @author jerryliu
 * @since 2015年8月14日 下午2:07:04
 */
public class UserInfoView extends LinearLayout {
    TextView infoTitleSub, infoTitle, infoTitleDetail;

    Button infoBtn;

    View item, infoDivider;

    public UserInfoView(Context context) {
        super(context);
    }

    public UserInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
        setFocusable(true);
        setClickable(true);
    }

    private void init(Context context, AttributeSet attrs) {
        View view = null;
        int orientation = SDKControler.getActivityOrientation();
        if (orientation == ActivityOrientation.PORTRAIT) {
            view = LayoutInflater.from(getContext()).inflate(
                    Rx.layout.kg_view_user_info_item_portrait, this);
        } else {
            view = LayoutInflater.from(getContext()).inflate(Rx.layout.kg_view_user_info_item_land,
                    this);
        }
        infoTitle = (TextView) view.findViewById(Rx.id.kg_info_title);
        infoTitleSub = (TextView) view.findViewById(Rx.id.kg_info_title_sub);
        infoTitleDetail = (TextView) view.findViewById(Rx.id.kg_info_detail);
        infoBtn = (Button) view.findViewById(Rx.id.kg_info_btn);
        infoDivider = view.findViewById(Rx.id.kg_info_item_divider);
        item = view.findViewById(Rx.id.kg_layout_item);
        String title = "";
        String titleDetail = "";
        boolean itemDividerVisibility = false;
        try {
            int[] resArray = {
                    Rx.attr.item_title, Rx.attr.item_divider_visible, Rx.attr.item_title_detail
            };
            TypedArray a = context.obtainStyledAttributes(attrs, resArray);
            title = a.getString(0);
            itemDividerVisibility = a.getBoolean(1, false);
            titleDetail = a.getString(2);

            // int[] resArray1 = Rx.styleable.kg_UserInfoView;
            // TypedArray a1 = context.obtainStyledAttributes(attrs, resArray1);
            // title = a.getString(Rx.styleable.kg_UserInfoView_item_title);
            // titleDetail =
            // a.getString(Rx.styleable.kg_UserInfoView_item_title_detail);
            // itemDividerVisibility =
            // a.getBoolean(Rx.styleable.kg_UserInfoView_item_divider_visible,
            // true);

            a.recycle();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 信息标题
        if (!TextUtils.isEmpty(title)) {
            infoTitle.setText(title);
        } else {
            infoTitle.setText("");
        }
        // 信息描述
        if (!TextUtils.isEmpty(titleDetail)) {
            infoTitleDetail.setVisibility(View.VISIBLE);
            infoTitleDetail.setText(titleDetail);
        } else {
            infoTitleDetail.setVisibility(View.GONE);
        }
        // 分割线
        infoDivider.setVisibility(itemDividerVisibility ? View.VISIBLE : View.GONE);

    }

    public UserInfoView setItemTitle(String title) {
        if (infoTitle != null) {
            infoTitle.setText(title);
        }
        return this;
    }

    public UserInfoView setItemDetail(String detail) {
        if (infoTitleDetail != null) {
            infoTitleDetail.setText(detail);
        }
        return this;
    }

    /**
     * 设置副标题内容
     * 
     * @param content
     */
    public UserInfoView setSubTitleText(String content) {
        if (infoTitleSub != null) {
            infoTitleSub.setVisibility(View.VISIBLE);
            infoTitleSub.setText(content);
        }
        return this;
    }

    public UserInfoView setBtnText(String text) {
        if (infoBtn != null) {
            infoBtn.setText(text);
        }
        return this;
    }

    public UserInfoView setDividerVisibility(int visibility) {
        if (infoDivider != null) {
            infoDivider.setVisibility(visibility);
        }
        return this;
    }

    public void setOnBtnClickListener(final OnClickListener listener) {
        if (infoBtn != null) {
            infoBtn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClick(v);
                    }
                }

            });
        }
    }

    public void setOnItemClickListener(final OnClickListener listener) {
        if (item != null) {
            item.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onClick(v);
                    }
                }

            });
        }
    }

}
